var TrunPage = {};
var paraObj = {};

//以下为getSystemConfig接口中返回 ---begin
var site_url = "";
var username = "";
var password = "";
var userroleid = "";
var IOSDeviceName = "";
//以下为getSystemConfig接口中返回 ---end

var paramMethod;
var getJsonMethod;
var web = new webView();
/**
 * "0": android; "1": IOS;2 web
 */
var clientType = "1";

/**
 * 从服务器端获取数据
 *
 * @param urlstr
 * @param onLoadServerDataSuccess
 */
function getJsonFormServer(urlstr, onLoadServerDataSuccess) {
    var type = "GET";
    var async = true;
    var dataType = "jsonp";
    var timeout = 90000;
    // 超时15秒限制
    var data;
    var url = "";
    if (urlstr.substring(0, 4) != 'http') {
        url = site_url + urlstr;
    } else {
        url = urlstr;
    }
    
    $.ajax({
           type : type,
           url : url,
           async : async,
           dataType : dataType,
           timeout : timeout,
           data : data,
           success : function(data, statu) {
           onLoadServerDataSuccess(data);
           },
           error : function(XMLHttpRequest, textStatus, errorThrown) {
           TrunPage.showToast("服务连接超时!"+textStatus+" "+errorThrown);
           }
           });
}

function getJsonFormServerHtml(urlstr, onLoadServerDataSuccess) {
    var returnType = "";
    var type = "GET";
    var async = true;
    var dataType = "html";
    var timeout = 90000;
    // 超时15秒限制
    var data="";
    var url = "";
    
    if (clientType == "0") {
        returnType = "returnType=android";
    } else if(clientType == "1") {
        returnType = "returnType=" + IOSDeviceName;
    }else{
        returnType = "returnType=browser";
    }
    
    if (url.indexOf("?") > 0) {
        url = url + "%26" + returnType;
    } else {
        url = url + "?" + returnType;
    }
    
    if (urlstr.substring(0, 4) != 'http') {
        url = site_url + urlstr;
    } else {
        url = urlstr;
    }
    
    $.ajax({
           type : type,
           url : url,
           async : async,
           dataType : dataType,
           timeout : timeout,
           data : data,
           success : function(data, statu) {
           onLoadServerDataSuccess(data);
           },
           error : function(XMLHttpRequest, textStatus, errorThrown) {
           TrunPage.showToast("服务连接超时!"+textStatus+" "+errorThrown);
           }
           });
}
function postFormServerHtml(urlstr, parameters, onLoadServerDataSuccess) {
    var returnType = "";
    var type = "POST";
    var async = true;
    var dataType = "html";
    var timeout = 90000;
    // 超时15秒限制
    var data = parameters;
    var url = "";
    
    if (clientType == "0") {
        returnType = "returnType=android";
    } else if(clientType == "1") {
        returnType = "returnType=" + IOSDeviceName;
    }else{
        returnType = "returnType=browser";
    }
    
    if (url.indexOf("?") > 0) {
        url = url + "%26" + returnType;
    } else {
        url = url + "?" + returnType;
    }
    
    if (urlstr.substring(0, 4) != 'http') {
        url = site_url + urlstr;
    } else {
        url = urlstr;
    }
    
    $.ajax({
           type : type,
           url : url,
           async : async,
           dataType : dataType,
           timeout : timeout,
           data : data,
           success : function(data, statu) {
           onLoadServerDataSuccess(data);
           },
           error : function(XMLHttpRequest, textStatus, errorThrown) {
           TrunPage.showToast("服务连接超时!"+textStatus+" "+errorThrown);
           }
           });
}

//解析参数
function parseRequest(paras){
    var paraString = paras.split("&");
    for (i=0; j=paraString[i]; i++){
        paraObj[j.substring(0,j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=")+1,j.length);
    }
    paramMethod();
}

//解析参数
function request(paras){
    var returnValue = paraObj[paras.toLowerCase()];
    if(typeof(returnValue)=="undefined"){
        return "";
    }else{
        return returnValue;
    }
}

function iosJson(dataJsonp){
    var data;
    if((typeof dataJsonp)=='string'){
        data = eval('(' + dataJsonp+ ')');
    }
    getJsonMethod(data);
}

TrunPage.pageInit = function() {
    if (clientType == "0") {
        loadPage();
    } else if(clientType == "1") {
        document.addEventListener("deviceready", getSystemConfig, false);
    }else{
        loadPage();
    }
}
function getSystemConfig()
{
    web.getSystemConfig(systemConfigSuccess);
}
function systemConfigSuccess(dataJsonp)
{
    var data;
    
	if((typeof dataJsonp)=='string'){
        
		data = eval('(' + dataJsonp+ ')');
        site_url = data.site_url;
        username = data.username;
        password = data.password;
        userroleid = data.userroleid;
        IOSDeviceName = data.devicename;
	}
    loadPage();
}

/**
 * 获取token参数.
 *
 * @param callback
 */
function getToken(callback) {
    TrunPage.showToast("tokenValue!");
    if (clientType == "0") {
        var tokenValue = android.openToken();
        TrunPage.showToast("tokenValue!"+tokenValue);
        callback(tokenValue);
    } else if(clientType == "1") {
        return web.getUuid(callback);
    }
}
/**
 * 初始化title中按钮的事件监听
 */
TrunPage.initClick = function() {
	if (clientType == "0") {
		android.initClick();
	}
}

TrunPage.callSystemAbility = function(systemAction) {
	if (clientType == "0") {
		android.initClick();
	}else if(clientType == "1"){
		web.callSystemAbility(systemAction);
	}
}

/**
 * js 中的调用方法，打开新的界面
 *
 * @param url
 *            请求页面地址
 * @param titletype
 *            新页面标题类型
 *
 * @param titletext
 *            标题名
 */
TrunPage.openWebView = function(url, titletype, titletext) {
	if (clientType == "0") {
		android.openWebView(url, titletype, titletext);
	} else if(clientType == "1") {
		web.openWebView(url, titletype, titletext);
	}else{
		url = url.substring(url.indexOf("/")+1, url.length);
		window.open(url);
	}
}
TrunPage.backToRootView = function() {
	if (clientType == "0") {
		//android.openWebView(url, titletype, titletext);
	} else if(clientType == "1") {
		web.backToRootView();
	}
}

TrunPage.checkInternetConnected = function(jscallback) {
	if (clientType == "0") {
		
	} else if(clientType == "1") {
		web.checkInternetConnected(jscallback);
	}else{
		url = url.substring(url.indexOf("/")+1, url.length);
		window.open(url);
	}
}
/**
 * js 中的调用方法，设置界面中native的title标签
 *
 * @param title
 *            标题名
 */
TrunPage.setTitleTxt = function(title) {
	if (clientType == "0") {
		android.setTitleTxt(title);
	} else if(clientType == "1") {
		web.setTitleTxt(title);
	}
}
TrunPage.setTitle = function(title) {
	if (clientType == "0") {
		android.setTitle(title);
	} else if(clientType == "1") {
		web.setTitleTxt(title);
	}
}

/**
 * js 中的调用方法，通过native处理网络请求数据，再将数据返回给js 参数 jscallback 回调函数 requestType 请求类型
 *
 * @param url
 *            访问URL
 * @param jscallback
 *            回调函数function名，字符串
 * @param requestType
 *            请求方式 'get', 'post'
 */
TrunPage.getJsonByGet = function(url, jscallback, requestType) {
	var returnType = "";
	if (clientType == "0") {
		returnType = "returnType=android";
	} else if(clientType == "1") {
		returnType = "returnType=" + IOSDeviceName;
	}else{
		returnType = "returnType=browser";
	}
	if (url.indexOf("?") > 0) {
		url = url + "&" + returnType;
	} else {
		url = url + "?" + returnType;
	}
    
	if (clientType == "0") {
		jscallback = jscallback + "";
		jscallback = jscallback.substring(8, jscallback.indexOf("("));
		android.getJsonByGet(url, jscallback + "", requestType);
	} else if(clientType == "1") {
		getJsonMethod = jscallback;
		web.getJsonByGet(iosJson, url, requestType);
	}else{
		getJsonFormServer(url,jscallback);
	}
}

TrunPage.returnData = function() {
	if (clientType == "0") {
		return android.returnData();
	}  else if(clientType == "1") {
		
	}
}
/**
 * 跳转本地浏览器.
 */
TrunPage.toSysBrowser = function(url) {
    if (clientType == "0") {
        android.toSysBrowser(url);
    } else if(clientType == "1") {
        web.toSysBrowser(url);
    }
}
/**
 * 设置title右侧按钮文字.
 */
TrunPage.setRightBtnText = function(txt) {
    if (clientType == "0") {
        android.setRightBtnText(txt);
    } else if(clientType == "1") {
        web.setRightBtnText(txt);
    }
}


TrunPage.setRightBtnImage = function(imgsrc) {
    if (clientType == "0") {
        android.setRightBtnImage(imgsrc);
    } else if(clientType == "1") {
        web.setRightBtnImage(imgsrc);
    }
}

/**
 * 获取传参值.
 */
TrunPage.getCallbackParams = function(jscallback) {
    paramMethod = jscallback;
    if (clientType == "0") {
        var CallbackParams = android.getCallbackParams();
        parseRequest(CallbackParams);
    } else if(clientType == "1") {
        web.getCallbackParams(parseRequest);
    } else {
        var url = location.href;
        var paraString = url.substring(url.indexOf("?")+1,url.length);
        parseRequest(paraString);
    }
}

/**
 * 获取手机串号.
 */
TrunPage.getUuid = function(jscallback) {
    if (clientType == "0") {
        var uuid = android.getUuid();
        jscallback(uuid);
    } else if(clientType == "1") {
        web.getUuid(jscallback);
    }
}
TrunPage.getUserName = function(jscallback) {
    if (clientType == "0") {
        
    } else if(clientType == "1") {
        web.getUserName(jscallback);
    }
}
TrunPage.getPassword = function(jscallback) {
    if (clientType == "0") {
        
    } else if(clientType == "1") {
        web.getPassword(jscallback);
    }
}

/**
 * 提示信息.
 */
TrunPage.showToast = function(message) {
    if (clientType == "0") {
        android.showToast(message);
    } else if(clientType == "1") {
        web.showToast(message);
    }else{
        alert(message);
    }
}

/**
 * 返回事件.
 */
TrunPage.goBack = function() {
    if (clientType == "0") {
        android.Return();
    } else if(clientType == "1") {
        web.goBack();
    }
    refreshPage();
}

TrunPage.refreshPrevPage = function() {
    if (clientType == "0") {
        
    } else if(clientType == "1") {
        web.refreshPrevPage();
    }
}

/**
 * 设置title右边按钮显示隐藏状态.
 */
TrunPage.setRightBtnVisible = function(booble) {
    if (clientType == "0") {
        android.setRightBtnVisible(booble);
    } else if(clientType == "1") {
        web.setRightBtnVisible(booble);
    }
}
TrunPage.setLeftBtnGoHistory = function(booble) {
    if (clientType == "0") {
        
    } else if(clientType == "1") {
        web.setLeftBtnGoHistory(booble);
    }
}

/**
 * js 中的调用方法，预览图片
 *
 * @param index
 *            图片序号（0,1,2,3）
 * @param images
 *            图片URL
 * @param names
 *            图片名
 * @param xs
 *            ys 经纬度
 */
TrunPage.showImage = function(index, images, names, xs, ys) {
    if (clientType == "0") {
        android.showImage(index, images, names, xs, ys);
    } else if(clientType == "1") {
        web.showImage(index, images, names, xs, ys);
    }
}

/**
 * 保存key value值.
 *
 * @param key
 * @param value
 */
TrunPage.setKeyValue = function(key, value) {
    if (clientType == "0") {
        android.setKeyValue(key, value);
    } else if(clientType == "1") {
        web.setKeyAndValue(key, value);
    }else{
        window.localStorage.setItem(key, value);
    }
}
/**
 * 根据key值返回value.
 *
 * @param key
 * @param jscallback
 *            回调函数
 */
TrunPage.getKeyValue = function(key, jscallback) {
    if (clientType == "0") {
        var value = android.getKeyValue(key);
        jscallback(value);
    } else if(clientType == "1") {
        web.getValueByKey(jscallback, key);
    }else{
        var paramValue = window.localStorage.getItem(key);
        window.localStorage.removeItem(key);
        jscallback(paramValue);
    }
}

/**
 * 去除/显示loading层. true 显示 false 隐藏
 */
TrunPage.setProgressBarVisibility = function(boolen) {
    if (clientType == "0") {
        android.setProgressBar(boolen);
    } else if(clientType == "1") {
        web.setProgressBarVisibility(boolen);
    }
}

/**
 * 获取经纬度.
 */
TrunPage.getXY = function(jscallback) {
    if (clientType == "0") {
        jscallback = jscallback + "";
        jscallback = jscallback.substring(8, jscallback.indexOf("("));
        android.getXY(jscallback);
    } else if(clientType == "1") {
        web.getXY(jscallback);
    }
}


TrunPage.getCityName = function(jscallback) {
    if (clientType == "0") {
        
    } else if(clientType == "1") {
        web.getCityName(jscallback);
    }else{
    }
}

/**
 * 闹铃提醒(确定)
 * @param roomStrId : 唯一表示一个提示的id,把会议室id+时间串 如："1003201302260900"
 * @param startTime : 会议的开始时间 如："2013-03-25 18:44"
 * @param content : 所要提示的内容 如："您预约的101会议室会议开始时间到，确定启用？ || 您预约的101会议室会议结束时间到，会议室空闲，确定续约？"
 */
TrunPage.setAlarmClock = function(roomStrId, conferenceTime, content) {
    if (clientType == "0") {
        android.setAlarmClock(roomStrId, conferenceTime, content);
    } else if(clientType == "1") {
        web.setAlarmClock(roomStrId, conferenceTime, content);
    }
}

/**
 * 闹铃提醒(取消)
 * @param roomStrId : 唯一表示一个提示的id,把会议室id+时间串 如："1003201302260900"
 * @param startTime : 会议的开始时间 如："2013-03-25 18:44"
 * @param content : 所要提示的内容 如：""
 */
TrunPage.cancelAlarmClock = function(roomStrId) {
    if (clientType == "0") {
        android.cancelAlarmClock(roomStrId);
    } else if(clientType == "1") {
        web.cancelAlarmClock(roomStrId);
    }
}

/**
 * 会议室撤销(确定，取消(取消就不进行操作))
 * @param msg : 提示信息的内容
 * @param clickConfirm : 点击确定的方法名
 */
TrunPage.dischargeConferenceOk = function(title,msg,clickConfirm) {
    if (clientType == "0") {
        android.showAlertDialog(title,msg);
    } else if(clientType == "1") {
        web.dischargeConferenceOk(title,msg,clickConfirm);
    }else{
        clickConfirm();
    }
}


/**
 * 调用相机.
 */
TrunPage.callSysCamera = function(jscallback) {
    if (clientType == "0") {
        jscallback = jscallback + "";
        jscallback = jscallback.substring(8, jscallback.indexOf("("));
        android.callSysCamera(jscallback);
    } else if(clientType == "1") {
        web.callSysCamera(jscallback);
    }
}
/**
 * 调用相册.
 */
TrunPage.callSysAlbum = function(jscallback) {
    if (clientType == "0") {
        jscallback = jscallback + "";
        jscallback = jscallback.substring(8, jscallback.indexOf("("));
        android.callSysAlbum(jscallback);
    } else if(clientType == "1") {
        web.callSysAlbum(jscallback);
    }
}



//获取当前应用内语言设置接口
//返回结果：0中文 1英文
TrunPage.getCurrLan = function(jscallback){
    if (clientType == "0") {
        android.getCurrLan();
    } else if(clientType == "1"){
        web.getCurrLan(jscallback);
    }
}

TrunPage.getUserRoleID = function(jscallback) {
    if (clientType == "0") {
    }
    else if(clientType == "1")
    {
        web.getUserRoleID(jscallback);
    }
}


//以下为移动OA专有接口

//**********//
TrunPage.setRightBtnGoHistory = function(booble) {
    if (clientType == "0") {
        android.setRightBtnGoHistory();
    } else if(clientType == "1") {
        web.setRightBtnGoHistory(booble);
    }
}

TrunPage.setRightBtnGoJS = function(boolen) {
    if (clientType == "0") {
        android.setRightBtnGoJS(boolen);
    } else if(clientType == "1") {
        web.setRightBtnGoJS();
    }
}
TrunPage.setLeftBtnText = function(txt) {
    if (clientType == "0") {
        android.setLeftBtnText(txt);
    } else if(clientType == "1") {
        web.setLeftBtnText(txt);
    }
}

TrunPage.setCancelProgressBar = function(boolen)
{
    if(clientType == "0")
    {
        
    }
    else
    {
        web.setCancelProgressBar(boolen);
    }
}

//获取cookies
TrunPage.getCookies=function(jscallback){
    if (clientType == "0") {
        var password = android.getCookies();
        jscallback(password);
    } else if(clientType == "1") {
        web.getCookies(jscallback);
    }
}


/**
 * 获取用户名加密
 */
TrunPage.getDesUserName = function(jscallback) {
    if (clientType == "0") {
        var userName = android.getDesUserName();
        jscallback(userName);
    } else if(clientType == "1") {
        web.getDesUserName(jscallback);
    }
}

/**
 * 获取密码加密
 */
TrunPage.getDesPassword = function(jscallback) {
    if (clientType == "0") {
        var password = android.getDesPassword();
        jscallback(password);
    } else if(clientType == "1") {
        web.getDesPassword(jscallback);
    }
}

/*
 获取云端token
 */
TrunPage.getCloudToken=function(jscallback){
    if (clientType == "0") {
        var password = android.getCloudToken();
        jscallback(password);
    } else if(clientType == "1") {
        web.getCloudToken(jscallback);
    }
}

//获取ServerUrl
TrunPage.getServerUrl=function(jscallback){
    if (clientType == "0") {
        var serverUrl = android.getServerUrl();
        jscallback(serverUrl);
    } else if(clientType == "1") {
        web.getServerUrl(jscallback);
    }
}

TrunPage.getPhoneType=function(jscallback){
    if (clientType == "0") {
    } else if(clientType == "1") {
        web.getPhoneType(jscallback);
    }
}

TrunPage.writeFileForCache=function(jscallback,applyName, fileName,content){
    if (clientType == "0") {
    } else if(clientType == "1") {
        web.writeFileForCache(jscallback,applyName, fileName,content);
    }
}

TrunPage.getNormalDes=function(jscallback,desString){
    if (clientType == "0") {
    } else if(clientType == "1") {
        web.getNormalDes(jscallback,desString);
    }
}

//获取移动oa的打开url
//获取正常事务加密
TrunPage.getPushUrl=function(jscallback){
    if(clientType=="0"){
        
    }else if(clientType=="1"){
        web.getPushUrl(jscallback);
    }
}

//学工二维码扫描
TrunPage.openQRCoderYX = function(jscallback) {
    if (clientType == "0") {
        
    } else if(clientType == "1") {
        web.openQRCoderYX(jscallback);
    }else{
    }
}
/*
 from to 是起始位置到结束位置坐标点的字符串
 latitude:longitude  纬度:经度
 */
TrunPage.mapNavigation = function(from,to) {
    if (clientType == "0") {
        
    } else if(clientType == "1") {
        web.mapNavigation(from,to);
    }else{
    }
}
/*
 locationArr字符串数组
 latitude:longitude:title  纬度:经度:标题
 */
TrunPage.mapShowAnnotations = function(locationArr) {
    if (clientType == "0") {
        
    } else if(clientType == "1") {
        web.mapShowAnnotations(locationArr);
    }else{
    }
}
/*
 打开附件接口:
 url:附件的地址
 name:附件的名字
 type:附件的类型
 */
TrunPage.openAttachment = function(url, name, type) {
    if (clientType == "0") {
        android.openAttachment(url, name, type);
    } else if(clientType == "1") {
        web.openAttachment(url, name, type);
    }else{
        url = url.substring(url.indexOf("/")+1, url.length);
        window.open(url);
    }
}